<template>
  <div>
    <!--导航-->
    <crumbs></crumbs>
    <el-row>
      <el-col :span="24">
        <el-button type="primary" @click="dialogVisible = !dialogVisible">添加用户</el-button>
      </el-col>
    </el-row>
    <!--表格 数据展示-->
    <role-table @showEditorDialog="showEditorDialog" ref="roleTable"></role-table>

    <!--添加用户对话框-->
    <el-dialog
            title="添加用户"
            :visible.sync="dialogVisible"
            width="30%"
            @closed="roleFormClose">
      <!--form-->
      <el-form label-width="80px" :model="roleForm" :rules="rules" ref="roleForm">
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="roleForm.roleName"></el-input>
        </el-form-item>
        <el-form-item label="角色介绍" prop="roleDesc">
          <el-input v-model="roleForm.roleDesc"></el-input>
        </el-form-item>

      </el-form>
      <!--footer-->
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUser">确 定</el-button>
      </span>
    </el-dialog>

    <!--编辑用户对话框-->
    <el-dialog
            title="添加用户"
            :visible.sync="dialogEditorVisible"
            width="30%">
      <!--form-->
      <el-form label-width="80px" :model="roleEditorForm" :rules="rules" ref="roleEditorForm">
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="roleEditorForm.roleName"></el-input>
        </el-form-item>
        <el-form-item label="角色介绍" prop="roleDesc">
          <el-input v-model="roleEditorForm.roleDesc"></el-input>
        </el-form-item>

      </el-form>
      <!--footer-->
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogEditorVisible = false">取 消</el-button>
        <el-button type="primary" @click="mdUser">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  // 面包屑导航
  import Crumbs from './navigation/Crumbs'
  import RoleTable from './roleTable/RoleTable'

  export default {
    name: "Role",
    components: {
      Crumbs,
      RoleTable
    },
    data() {
      return {
        roleForm: {
          roleName: '',
          roleDesc: ''
        },
        roleEditorForm: {
          roleId: null,
          roleName: '',
          roleDesc: ''
        },
        dialogVisible: false,
        dialogEditorVisible: false,
        rules: {
          roleName: [
            { required: true, message: '请输入用户名称', trigger: 'blur' },
            { min: 3, max: 6, message: '长度在 3 到 6 个字符', trigger: 'blur' }
          ],
          roleDesc: [
            { required: true, message: '请输入用户介绍', trigger: 'blur' }
          ],
        }
      }
    },
    methods: {
      //展示修改用户对话框 , 并且获取user
      showEditorDialog(user) {
        this.$http.get("roles/" + user.id).then(result => {
          if (result.data.meta.status !== 200) return this.$message.error("获取数据失败")
          this.roleEditorForm = result.data.data
          console.log(this.roleEditorForm);
        })
        this.dialogEditorVisible = true
      },
      // 添加用户
      addUser() {
        this.$refs.roleForm.validate(valid => {
          if (!valid) return
          this.$http.post('/roles',this.roleForm).then(result => {
            if (result.data.meta.status !== 201) return this.$message.error("添加用户失败")
            this.dialogVisible = false
            this.$refs.roleTable.getRoles()
            return this.$message.success("添加用户成功")
          })
        })
      },
      mdUser() {
        console.log(this.roleEditorForm.roleId);
        this.$refs.roleEditorForm.validate(valid => {
          if (!valid) return
          this.$http.put('/roles/' + this.roleEditorForm.roleId,this.roleEditorForm).then(result => {
            if (result.data.meta.status !== 200) return this.$message.error("修改用户失败")
            this.dialogEditorVisible = false
            this.$refs.roleTable.getRoles()
            return this.$message.success("修改用户成功")
          })
        })
      },
      roleFormClose() {
        this.$refs.roleForm.resetFields()
      }
    }
  }
</script>

<style scoped>

</style>